//网络请求
import axios from 'axios';
import AppConfig from '@/constants/AppConfig.js';

const $axios = axios;

export default function() {
	var $app_config = AppConfig();
	var $base_url = $app_config.Base_Url;
	var $post_config = $app_config.POST_Cfg;
	//登录
	var $url_login = $base_url + "/admin/account/v1/login";
	//发送验证码
	var $url_sendVCode = $base_url + "/admin/account/v1/sendVCode";
	//验证验证码
	var $url_vertifyVCode = $base_url + "/admin/account/v1/vertifyVCode";
	//修改密码
	var $url_findPassword = $base_url + "/admin/account/v1/findPassword";
	// 管理员接口
	var $url_getAccounts = $base_url + "/admin/account/v1/getAccounts";
	var $url_edit = $base_url + "/admin/account/v1/edit";
	var $url_editPwd = $base_url + "/admin/account/v1/editPwd";
	var $url_enable = $base_url + "/admin/account/v1/enable";
	var $url_disable = $base_url + "/admin/account/v1/disable";
	
	return {
		login: function(username, password) {
			return new Promise(function(resolve, reject) {
				$axios.post($url_login, {
						username: username,
						password: password
					}, $post_config)
					.then(function(response) {
						resolve(response);
					})
					.catch(function(err) {
						reject(err);
					});
			});
		},
		sendVCode: function(phone, type) {
			return new Promise(function(resolve, reject) {
				$axios.post($url_sendVCode, {
						phone: phone,
						type: type
					}, $post_config)
					.then(function(response) {
						resolve(response);
					})
					.catch(function(err) {
						reject(err);
					});
			});
		},
		vertifyVCode: function(phone, type, vcode) {
			return new Promise(function(resolve, reject) {
				$axios.post($url_vertifyVCode, {
						phone: phone,
						type: type,
						vcode: vcode
					}, $post_config)
					.then(function(response) {
						resolve(response);
					})
					.catch(function(err) {
						reject(err);
					});
			});
		},
		findPassword: function(tempToken, pwd, submitPwd) {
			return new Promise(function(resolve, reject) {
				$axios.post($url_findPassword, {
						tempToken: tempToken,
						pwd: pwd,
						submitPwd: submitPwd
					}, $post_config)
					.then(function(response) {
						resolve(response);
					})
					.catch(function(err) {
						reject(err);
					});
			});
		},
		//获取管理员列表
		getAccounts: function(key, roleType, page, pagesize) {
			return new Promise(function(resolve, reject) {
				$axios.post($url_getAccounts, {
						key: key,
						roleType: roleType,
						page: page,
						pagesize: pagesize
					}, $post_config)
					.then(function(response) {
						console.log(response);
						resolve(response);
					})
					.catch(function(err) {
						reject(err);
					});
			});
		},
		//新增、编辑账户
		edit: function(params) {
			return new Promise(function(resolve, reject) {
				$axios.post($url_edit, {
						accid: params.accid,
						username: params.username,
						phoneNum: params.phoneNum,
						roleType: params.roleType,
						name: params.name
					}, $post_config)
					.then(function(response) {
						console.log(response);
						resolve(response);
					})
					.catch(function(err) {
						reject(err);
					});
			});
		},
		//重置密码
		editPwd: function(accid, password) {
			return new Promise(function(resolve, reject) {
				$axios.post($url_editPwd, {
						accid: accid,
						password: password
					}, $post_config)
					.then(function(response) {
						console.log(response);
						resolve(response);
					})
					.catch(function(err) {
						reject(err);
					});
			});
		},
		// 启用
		enable: function(accid) {
			return new Promise(function(resolve, reject) {
				$axios.post($url_enable, {
						accid: accid
					}, $post_config)
					.then(function(response) {
						console.log(response);
						resolve(response);
					})
					.catch(function(err) {
						reject(err);
					});
			});
		},
		// 禁用
		disable: function(accid) {
			return new Promise(function(resolve, reject) {
				$axios.post($url_disable, {
						accid: accid
					}, $post_config)
					.then(function(response) {
						console.log(response);
						resolve(response);
					})
					.catch(function(err) {
						reject(err);
					});
			});
		},
	}
}